home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Scripts / CompareFiles < prev    next >
Encoding:
Text File  |  1996-04-11  |  13.2 KB  |  373 lines  |  [TEXT/MPS ]

  1. #    CompareFiles - Compare two source files and show the differences
  2. #
  3. #    Version 3.4.1b3
  4. #
  5. #    Usage:
  6. #        CompareFiles [-TopDown] [-9 | -LC -13 | -16 | -Portrait | -TwoPage | -b x y | -r t,l,b,r] oldFile newFile    - open up the files and add menu
  7. #            -9            use 9 inch screen (512 x 342)
  8. #            -13            use 13 inch screen (640 x 480)
  9. #            -16            use 16 inch screen (832 x 624)
  10. #            -17                    use 17 inch screen Multiscan at max scan (1024 x 768)
  11. #            -LC            use 12 inch screen (
  12. #            -Portrait    use 15 inch screen (640 x 870)
  13. #            -TwoPage    use 21 inch screen (1152 x 870)
  14. #            -b x y        use screen dimensions given in x y
  15. #            -r t,l,b,r    use screen rect given by Top,Left,Bottom,Right
  16. #            -TopDown        allow editing from top to bottom rather than bottom to top.
  17. #
  18. #            The default is to use the dimensions of the main monitor.
  19. #
  20. #        NOTE:  
  21. #            No error checking is done on x y coordinates
  22. #
  23. #    Returns:
  24. #        Status = 0 - files match
  25. #        Status = 1 - syntax error
  26. #        Status = 2 - files differ  (this is the same status as the Compare tool)
  27. #
  28. #    CompareFiles will compare two files, and if they don't match it will bring them
  29. # both up on the screen and put them side by side.  A menu will be appended to the
  30. # menu bar to go through the changes.  When all the changes have been shown, the
  31. # file windows will be restored and closed.
  32. #
  33. # In addition to the menu the following keys are provided to do the menu operations:
  34. #
  35. #            Control+UpArrow             Moves to the next  mismatch
  36. #            Control+DownArrow         Moves to the next  mismatch (same as Control+UpArrow)
  37. #            Control+RightArrow         Copy selection from left file to right file
  38. #            Control+LeftArrow         Copy selection from right file to left file
  39. #
  40. #    If the -TopDown option is not specified the differences are shown from bottom to top
  41. # so that if any changes are made in the files the line offsets are not messed up.  If
  42. # -TopDown is specified, the changes CAN be made from the top down due to the fact that
  43. # the files are recompared before moving to the next mismatch.  Although this may sound
  44. # more inefficient than bottom-up comparing, it is in fact faster!  This is because the
  45. # Compare tool is very fast plus it only has to read the files starting at the previous
  46. # mismatch and quits as soon as the next mismatch is found.  This is the more natural
  47. # way to edit the files.  Bottom-up processing is supported (as the default) only for
  48. # compatibility.  It's slower since a helper script must be executed to search a file
  49. # of Compare output for the previous mismatch.
  50. #
  51. #    When not using -TopDown, the temporary file {TempFolder}Compare_File is created to hold
  52. # the Compare output.  This file are deleted on exit.
  53. #
  54. #    Copyright Apple Computer, Inc. 1988 - 1996
  55. #    All rights reserved.
  56. #
  57. #    Version 3.3 by Peter Potrebic with enhancements by Greg Branche
  58. #    Version 3.4.1 enhancements by Ira L. Ruben
  59. #
  60. # Helper script and tools referenced by this script:
  61. #
  62. #        CompareFiles•NextChange - script to show next change (used when not using -Topdown)
  63. #        Monitors                                - command to get default main screen dimensions
  64. #
  65. # Implementation details -- The -TopDown option assumes the new version of Compare which
  66. #                                                      supports the -selection option for getting and setting
  67. #                                                      selections.
  68. #
  69. #                                                      The Monitors command is a MPW command that is used by
  70. #                                                      this script to get the dimensions of the main monitor as the
  71. #                                                      default display size.
  72. #
  73.  
  74. Set Exit 0
  75. Set CaseSensitive 0
  76. Set ErrorMessage     "### CompareFiles 3.4.1b3 - Incorrect order, or missing or illegal option"
  77. Set Usage             "# Usage - CompareFiles [-TopDown] [-9 | -LC | -13 | -16 | [-17] | -Portrait | -TwoPage | -b x y | -r t,l,b,r] oldFile newFile"
  78.  
  79. Set CompareOptions "-g 3 -b -m -t {DefaultCompareFilesOptions}"
  80. UnSet File1
  81. UnSet File2
  82.  
  83. if 0
  84. Set Top    0
  85. Set Left      7                                                                        # default the left edge of the windows
  86. Set Bottom 345
  87. Set Right  626
  88. Set CompareFilesTileRectangle "0, 5, 344, 635"
  89. else
  90. (Evaluate "`Monitors`" =~ /(≈)®1,(≈)®2,(≈)®3,(≈)®4/) >Dev:Null
  91. Set Top         "{®1}"
  92. Set Left         "{®2}"
  93. Set Bottom     "{®3}"
  94. Set Right     "{®4}"
  95. Evaluate Left   = {Left} + 5
  96. Evaluate Right  = {Right} - 4
  97. Evaluate Bottom = {Bottom} - 142
  98. Set CompareFilesTileRectangle "{Top}, {Left}, {Bottom}, {Right}"
  99. Evaluate Bottom = {Bottom} + 1
  100. Evaluate Right  = {Right} - 9
  101. end
  102.  
  103. Set TopDown 0
  104.  
  105. loop                                                                                        # process the options...
  106.     Break if {#} == 0
  107.     If !{#}                                                                                # Done?
  108.         If !"{File2}"
  109.             Alert "Must specify two (and only two) files.∂n∂n{Usage}"
  110.             Exit 1
  111.         End
  112.         Break
  113.     End
  114.     
  115.     If "{1}" !~ /-≈/                                                            # Filename?
  116.         If !"{File1}"
  117.             Set File1 "{1}"
  118.             Shift
  119.             Continue
  120.         end
  121.         If !"{File2}"
  122.             Set File2 "{1}"
  123.             Shift
  124.             Continue
  125.         end
  126.         Alert "Must specify two (and only two) files.∂n∂n{Usage}"
  127.         Exit 1
  128.     End
  129.     If "{1}" =~ /-td/    || "{1}" =~ /-topdown/            # -topdown
  130.         Set TopDown 1
  131.         Shift 1
  132.         Continue
  133.     End
  134.     If "{1}" =~ /-TwoPage/                                                # -TwoPage
  135.         Set CompareFilesTileRectangle "0, 5, 727, 1148"
  136.         Set Bottom 728
  137.         Set Right 1139
  138.         Shift 1
  139.         Continue
  140.     End
  141.     If "{1}" =~ /-17/                                                            # -17
  142.         Set CompareFilesTileRectangle "0, 5, 625, 1020"
  143.         Set Bottom 626
  144.         Set Right  1011
  145.         Shift 1
  146.         Continue
  147.     End
  148.     If "{1}" =~ /-Portrait/                                                # -Portrait
  149.         Set CompareFilesTileRectangle "0, 5, 754, 635"
  150.         Set Bottom 755
  151.         Set Right  626
  152.         Shift 1
  153.         Continue
  154.     End
  155.     If "{1}" == "-16"                                                            # -16 (Apple 16" screen)
  156.         Set CompareFilesTileRectangle "0, 5, 482, 828"
  157.         Set Bottom 483
  158.         Set Right  819
  159.         Shift 1
  160.         Continue
  161.     End
  162.     If "{1}" =~ /-13/                                                            # -13 (MacII screen)
  163.         Set CompareFilesTileRectangle "0, 5, 344, 635"
  164.         Set Bottom 345
  165.         Set Right  626
  166.         Shift 1
  167.         Continue
  168.     End
  169.     If "{1}" =~ /-LC/                                                            # -LC (LC 12" RGB screen)
  170.         Set CompareFilesTileRectangle "0, 3, 240, 510"
  171.         Set Bottom 241
  172.         Set Right  500
  173.         Shift 1
  174.         Continue
  175.     End
  176.     If "{1}" == "-9"                                                            # -9 (Mac+ and MacSE screen)
  177.         Set CompareFilesTileRectangle "0, 3, 198, 510"
  178.         Set Bottom 199
  179.         Set Right  500
  180.         Shift 1
  181.         Continue
  182.     End
  183.     If "{1}" == "-b" || "{1}" == "-B"                            # -b x y
  184.         Evaluate Right = {2} - 5
  185.         Evaluate Bottom = {3} - 140
  186.         Set CompareFilesTileRectangle "0,5,{Bottom},{Right}"
  187.         Evaluate Bottom = {Bottom} + 1
  188.         Evaluate Right = {Right} - 9
  189.         Shift 3
  190.         Continue
  191.     End
  192.     If "{1}" == "-r" || "{1}" == "-R"                            # -r t,l,b,r
  193.         (Evaluate "{2}" =~ /(≈)®1,(≈)®2,(≈)®3,(≈)®4/) >Dev:Null
  194.         Set Top "{®1}"
  195.         Set Left "{®2}"
  196.         Set Bottom "{®3}"
  197.         Set Right "{®4}"
  198.         Evaluate Bottom = {Bottom} - 120
  199.         Set CompareFilesTileRectangle "{Top}, {Left}, {Bottom}, {Right}"
  200.         Evaluate Bottom = {Bottom} + 1
  201.         Evaluate Left = {Left} + 2
  202.         Evaluate Right = {Right} - {Left} - 2
  203.         Shift 2
  204.         Continue
  205.     end
  206.         Alert "Invalid option ("{1}")∂n∂n{Usage}"
  207.         exit 1
  208. end
  209.  
  210. if !"{File1}" || !"{File2}"
  211.     Alert "Must specify two (and only two) files.∂n∂n{Usage}"
  212.     Exit 1
  213. end
  214.  
  215. # add the ability to specify a filename and a directory
  216. # the same named file from the two directories is then compared
  217.  
  218. set File1Directory "`Exists -d "{File1}"`" 
  219. if ("{File1Directory}"  != "")
  220.     set File1 "{File1Directory}{File2}"
  221. end
  222. set File2Directory "`exists -d "{File2}"`" 
  223. if ("{File2Directory}"  != "")
  224.     set File2 "{File2Directory}{File1}"
  225. end
  226.  
  227. if  ("{File1Directory}"  != "") && ("{File2Directory}"  != "")
  228.     Alert "Must specify at least one file and one directory file.∂n∂n{Usage}"
  229.     exit 1
  230. end
  231.     
  232.  
  233. if {TopDown}
  234.     Evaluate Bottom = {Bottom} + 119
  235.     Evaluate Right  = {Right}  + 9
  236.     Set CompareFilesTileRectangle "{Top}, {Left}, {Bottom}, {Right}"
  237.     Evaluate Bottom = {Bottom} + 1
  238.     Evaluate Right  = {Right}  - 9
  239. end
  240.  
  241. Set ActiveAtStart "{Active}"
  242. Set TargetAtStart "{Target}"
  243.  
  244. Set CompareStatus 0
  245. Compare {CompareOptions} "{{File1}}" "{{File2}}" > "{TempFolder}"Compare_File || Set CompareStatus {Status}
  246.  
  247. if {CompareStatus} == 0
  248.     Echo "### files are equal (ignoring blanks): {File1} - {File2}"
  249.     Delete -i "{TempFolder}Compare_File" ≥ dev:null
  250. else if {CompareStatus} == 2 || {CompareStatus} == 4    # files don’t match - set everything up
  251.     if !{TopDown}
  252.     StreamEdit "{{TempFolder}}"Compare_File -o "{{TempFolder}}"Compare_File ∂
  253.         -e '/•[ ∂t]*File (≈)®1; Line (≈)®2; File (≈)®3; Line (≈)®4/ Change "    Find "®2" "®1"; Find "®4" "®3'
  254.     Set NothingMatches 1
  255.     Search -s /•∂*∂*∂* Nothing seems to match ∂*∂*∂*/ "{{TempFolder}}"Compare_File ∑∑dev:null || Set NothingMatches 0
  256.     else if {CompareStatus} == 4
  257.         Set NothingMatches 1
  258.     else
  259.         Set NothingMatches 0
  260.     end
  261.     If {NothingMatches}
  262.         Beep
  263.         Confirm "Nothing matches!  Continue the file comparison?"
  264.         If {Status} == 4
  265.             if !{TopDown}
  266.                 Delete -i "{{TempFolder}}"Compare_File
  267.             end
  268.             Exit 3
  269.         End
  270.     End
  271.  
  272.     Set File1 "`Files -f -q "{File1}"`"            # get full pathnames
  273.     Set File2 "`Files -f -q "{File2}"`"
  274.     
  275.     Unset Close1 Close2                 # remember if either file is already open
  276.     Set openWindows " ``Windows -q`` "
  277.     If "{openWindows}" !~ /≈ [∂']*"{File1}"[∂']* ≈/; Open "{File1}"; Set Close1 1; End
  278.     If "{openWindows}" !~ /≈ [∂']*"{File2}"[∂']* ≈/; Open "{File2}"; Set Close2 1; End
  279.     
  280.     # Save current selection so we can restore it when we're done
  281.     Mark -y § __cfSelectionOnEntry__ "{{File1}}" ≥dev:null
  282.     Mark -y § __cfSelectionOnEntry__ "{{File2}}" ≥dev:null
  283.     
  284.     set W1location "`MoveWindow "{{File1}}";echo ∂;;SizeWindow "{{File1}}"`"
  285.     set W2location "`MoveWindow "{{File2}}";echo ∂;;SizeWindow "{{File2}}"`"
  286.     
  287.     if !{TopDown}
  288.         begin 
  289.             Echo Begin∂;                                                                                                                ;∂
  290.             Echo Set Exit 0∂;                                                                                                        ;∂
  291.             Echo DeleteMenu Compare∂;                                                                                        ;∂
  292.             Echo SetKey Control-RightArrow Beep∂;                                                                ;∂
  293.             Echo SetKey Control-LeftArrow Beep ∂;                                                                ;∂
  294.             Echo SetKey Control-DownArrow Beep ∂;                                                                ;∂
  295.             Echo SetKey Control-UpArrow Beep ∂;                                                                    ;∂
  296.             Quote Close -y "{{TempFolder}}Compare_File"; Echo ∂;                                ;∂
  297.             Quote Delete -i "{{TempFolder}}Compare_File"; Echo ∂;                                ;∂
  298.             Echo "{W1location}"; Echo ∂;                                                                                ;∂
  299.             Quote Find __cfSelectionOnEntry__ "{{File1}}"; Echo ∂;                            ;∂
  300.             Quote Unmark __cfSelectionOnEntry__ "{{File1}}" ≥dev:null; Echo ∂;    ;∂
  301.             If {Close1}; Quote Close "{{File1}}"; Echo ∂;; End                                    ;∂
  302.             Echo "{W2location}"; Echo ∂;                                                                                ;∂
  303.             Quote Find __cfSelectionOnEntry__ "{{File2}}"; Echo ∂;                            ;∂
  304.             Quote Unmark __cfSelectionOnEntry__ "{{File2}}" ≥dev:null; Echo ∂;    ;∂
  305.             If {Close2}; Quote Close "{{File2}}"; Echo ∂;; End                                    ;∂
  306.             Quote Open "{ActiveAtStart}"; Echo ∂;                                                                ;∂
  307.             If "{TargetAtStart}"                                                                                                ;∂
  308.                 Quote Open -t "{TargetAtStart}"; Echo ∂;                                                    ;∂
  309.             End                                                                                                                                    ;∂
  310.             Echo End ∂∑ Dev:Null;
  311.         End > "{{TempFolder}}Compare_State"
  312.     else
  313.         begin
  314.             Echo Begin∂;                                                                                                                ;∂
  315.             Echo Set Exit 0∂;                                                                                                        ;∂
  316.             Echo DeleteMenu Compare∂;                                                                                        ;∂
  317.             Echo SetKey Control-RightArrow Beep∂;                                                                ;∂
  318.             Echo SetKey Control-LeftArrow Beep ∂;                                                                ;∂
  319.             Echo SetKey Control-DownArrow Beep ∂;                                                                ;∂
  320.             Echo SetKey Control-UpArrow Beep ∂;                                                                    ;∂
  321.             Echo "{W1location}"; Echo ∂;                                                                                ;∂
  322.             Quote Find __cfSelectionOnEntry__ "{{File1}}"; Echo ∂;                            ;∂
  323.             Quote Unmark __cfSelectionOnEntry__ "{{File1}}" ≥dev:null; Echo ∂;    ;∂
  324.             If {Close1}; Quote Close "{{File1}}"; Echo ∂;; End                                    ;∂
  325.             Echo "{W2location}"; Echo ∂;                                                                                ;∂
  326.             Quote Find __cfSelectionOnEntry__ "{{File2}}"; Echo ∂;                            ;∂
  327.             Quote Unmark __cfSelectionOnEntry__ "{{File2}}" ≥dev:null; Echo ∂;    ;∂
  328.             If {Close2}; Quote Close "{{File2}}"; Echo ∂;; End                                    ;∂
  329.             Quote Open "{ActiveAtStart}"; Echo ∂;                                                                ;∂
  330.             If "{TargetAtStart}"                                                                                                ;∂
  331.                 Quote Open -t "{TargetAtStart}"; Echo ∂;                                                    ;∂
  332.             End                                                                                                                                    ;∂
  333.             Echo End ∂∑ Dev:Null;
  334.         End > "{{TempFolder}}Compare_State"
  335.     end
  336.  
  337.     TileWindows -v -r "{CompareFilesTileRectangle}" "{{File1}}" "{{File2}}"
  338.     
  339.     if !{TopDown}
  340.         Open -t "{{TempFolder}}"Compare_File
  341.         MoveWindow {Left} {Bottom} "{TempFolder}"Compare_File
  342.         SizeWindow {Right} 75 "{TempFolder}"Compare_File
  343.         Find ∞ "{{TempFolder}}"Compare_File
  344.     end
  345.  
  346.     if !{TopDown}
  347.         AddMenu Compare "Find Next Change/ƒ"    "CompareFiles•NextChange"
  348.         SetKey  Control-DownArrow                            "CompareFiles•NextChange"
  349.         SetKey  Control-UpArrow                                "CompareFiles•NextChange"
  350.     else
  351.         AddMenu Compare 'Compare Again'                'Compare '"{CompareOptions}"' -selection set  "'"{{File1}}"'" "'"{{File2}}"'" && Alert The files match.'
  352.         AddMenu Compare    "(-0"                    ""
  353.         AddMenu Compare 'Find Next Change/ƒ'    'Compare '"{CompareOptions}"' -selection next "'"{{File1}}"'" "'"{{File2}}"'" && Alert No more differences.'
  354.         SetKey  Control-DownArrow                            'Compare '"{CompareOptions}"' -selection next "'"{{File1}}"'" "'"{{File2}}"'" && Alert No more differences.'
  355.         SetKey  Control-UpArrow                                'Compare '"{CompareOptions}"' -selection next "'"{{File1}}"'" "'"{{File2}}"'" && Alert No more differences.'
  356.     end
  357.     AddMenu    Compare "(-1" ""
  358.     AddMenu Compare "Copy Selection »»"            "Catenate ∂"{{File1}}∂".§ > ∂"{{File2}}∂".§"
  359.     SetKey  Control-RightArrow                            "Catenate ∂"{{File1}}∂".§ > ∂"{{File2}}∂".§"
  360.     AddMenu Compare "«« Copy Selection"            "Catenate ∂"{{File2}}∂".§ > ∂"{{File1}}∂".§"
  361.     SetKey  Control-LeftArrow                                "Catenate ∂"{{File2}}∂".§ > ∂"{{File1}}∂".§"
  362.     AddMenu    Compare "(-2" ""
  363.     AddMenu    Compare "Done"                                    "CompareFiles•Quit"
  364.  
  365.     if !{TopDown}
  366.         CompareFiles•NextChange
  367.     else
  368.         Compare {CompareOptions} -selection set "{{File1}}" "{{File2}}" && Alert 'The files match.'
  369.     end
  370. end
  371.  
  372. Exit {CompareStatus}
  373.